<?php
namespace Goatherd\Convert;

/**
 *
 * @todo concept details, check concept
 * @todo should mbstring extension really be required?
 */
abstract class AbstractTextConverter
implements IConverter
{
    /**
     *
     * @var string
     */
    protected $_detectOrder = null;

    /**
     *
     * @var string
     */
    protected $_defaultEncoding = null;

    public function __construct()
    {
        $this->init();
    }

    public function init()
    {
        $this->_detectOrder = mb_detect_order();
        $this->_defaultEncoding = mb_internal_encoding();
    }

    public function setDetectOrder($order)
    {
        // TODO set detect order
    }

    public function getDetectOrder()
    {

    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Convert.IConverter::convert()
     */
    public function convert($data, $in_encoding = null, $out_encoding = null)
    {
        // empty content
        if ($content == '') {
            return (string) $content;
        }

        // detect encoding (if none provided)
        if (!is_string($in_encoding)) {
            $in_encoding = mb_detect_encoding($content, $this->_detectOrder , true);
        }

        // out encoding
        null === $out_encoding and $out_encoding = $this->_defaultEncoding;

        // fix case
        $in_encoding = strtoupper($in_encoding);
        $out_encoding = strtoupper($out_encoding);

        return $this->_convert($data, $in_encoding, $out_encoding);
    }

    abstract protected function _convert($data, $in_encoding, $out_encoding);

    /**
     * (non-PHPdoc)
     * @see Goatherd\Convert.IConverter::normalise()
     */
    public function normalise($data);

    /**
     * (non-PHPdoc)
     * @see Goatherd\Convert.IConverter::compare()
     */
    public function compare($data1, $data2);

    /**
     * (non-PHPdoc)
     * @see Goatherd\Convert.IConverter::__invoke()
     */
    public function __invoke($data)
    {
        return $this->convert($data);
    }
}